package com.cxc.minio.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cxc.minio.entity.UserContract;
import com.cxc.minio.entity.UserImage;
import com.cxc.minio.entity.UserInfo;
import com.cxc.minio.mapper.UserContractMapper;
import com.cxc.minio.mapper.UserImageMapper;
import com.cxc.minio.service.UserInfoService;
import com.cxc.minio.mapper.UserInfoMapper;
import io.minio.MinioClient;
import io.minio.RemoveObjectArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
* @author cxc
* @description 针对表【t_user_info】的数据库操作Service实现
* @createDate 2025-01-04 16:42:28
*/
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>
    implements UserInfoService{

    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private UserImageMapper  userImageMapper;
    @Resource
    private UserContractMapper contractMapper;
    @Resource
    private MinioClient minioClient;

    @Override
    public UserInfo getUserInfoById(Integer id) {
        return userInfoMapper.selectUserInfoById(id);
    }

    @Override
    public List<UserInfo> getUserList() {
        return userInfoMapper.selectUserList();
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean delUserById(Integer id) throws Exception{


        UserInfo userInfo = userInfoMapper.selectUserInfoById(id);
        /**
         * 删除用户信息
         */
        int deleteUser = userInfoMapper.deleteById(id);
        /**
         * 删除用户头像
         */
        LambdaQueryWrapper<UserImage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserImage::getUid,id);
        userImageMapper.delete(wrapper);

        /**
         * 删除文件
         */
        LambdaQueryWrapper<UserContract> userContractWrapper = new LambdaQueryWrapper<>();
        userContractWrapper.eq(UserContract::getUid,id);
        contractMapper.delete(userContractWrapper);

        /**
         * 删除minid
         */

        if (null !=userInfo && null != userInfo.getUserImageDO()) {
            //删除MinIO服务器上的文件 (删除头像)
            minioClient.removeObject(RemoveObjectArgs.builder()
                    .bucket(userInfo.getUserImageDO().getBucket())
                    .object(userInfo.getUserImageDO().getObject())
                    .build());
        }

        if (null !=userInfo && null != userInfo.getUserContractDO()) {
            //删除MinIO服务器上的文件 (删除合同)
            minioClient.removeObject(RemoveObjectArgs.builder()
                    .bucket(userInfo.getUserContractDO().getBucket())
                    .object(userInfo.getUserContractDO().getObject())
                    .build());
        }

        return deleteUser >= 1;
    }
}




